package cs3343.project.family.UI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cs3343.project.family.Character;
import cs3343.project.family.CharacterController;
import cs3343.project.family.ConfigManager;
import cs3343.project.family.GameController;
import cs3343.project.family.MapController2;
import cs3343.project.family.ProfileManager;

public class CharacterFrame extends JFrame implements ActionListener, ItemListener {
	
	private GameController gc;

	private JPanel contentPane;
	
	private JButton btn_createProfile;
	
	private Icon enter;
	
	private JButton btn_startGame;
	
	private JButton btn_gameOption;
	
	public static Character[] character = new Character[6];
	
	private JComboBox[] comboList;
	private JComboBox<String> combo = null;
	
	int comboActivateNum = 0; 

	/**
	 * Create the frame.
	 */
	public CharacterFrame(GameController gc) {
		this.gc = gc;
		
		initUI();
		prepareProfileList();
		
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(2,1));
		panel.setBorder(BorderFactory.createTitledBorder("Family - Character")); 
		JPanel title = new JPanel();
		JPanel playerPane = new JPanel();
		title.setLayout(new BorderLayout());
		playerPane.setLayout(new GridLayout(2,3));
		
		btn_createProfile = new JButton();
		btn_createProfile.setText("Create Profile");
		btn_createProfile.addActionListener(this);
		
		enter =  new ImageIcon(ConfigManager.IMAGE_PATH + "family.jpg");
		btn_startGame = new JButton();

		btn_startGame.setRolloverEnabled(true);
		btn_startGame.setIcon(enter);
		btn_startGame.addActionListener(this);
		
		btn_gameOption = new JButton();
		btn_gameOption.setText("Game Setting");
		
		title.add(btn_createProfile, BorderLayout.WEST);
		title.add(btn_startGame, BorderLayout.CENTER);
		title.add(btn_gameOption, BorderLayout.EAST);
		title.setBorder(BorderFactory.createRaisedBevelBorder());
		
		
		
		
		
		//Character Choose Box
		JPanel playerBox = null;
		JLabel picture = null;
		comboList = new JComboBox[ConfigManager.MAX_PLAYER];

		for (int i = 0; i < ConfigManager.MAX_PLAYER; i++) {
			playerBox = new JPanel();
			playerBox.setLayout(new BorderLayout());
			//TODO show pic of selected char
			//picture = new JLabel(new ImageIcon(ConfigManager.IMAGE_PATH + ""+i+".jpg"));
			picture = new JLabel(new ImageIcon(ConfigManager.IMAGE_PATH + "ran.jpg"));
			combo = new JComboBox<String>(ProfileManager.profileArr);
			combo.addItemListener(this);
			
			if (i != 0)
				combo.setEnabled(false);
			
			comboList[i] = combo;
			playerBox.setBorder(BorderFactory.createTitledBorder("-Choose Character-"));
			playerBox.add(picture,BorderLayout.CENTER);
			if (i < ConfigManager.MAX_PLAYER)
				playerBox.add(combo,BorderLayout.SOUTH);
			playerPane.add(playerBox);
		}
		
		panel.add(title);
		panel.add(playerPane);
		contentPane.add(panel);
	}
	
	private void initUI() {
		setResizable(false);
		setTitle("Family");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(300, 20, 800, 700);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
	}
	
	private void prepareProfileList() {
		ProfileManager.readFromProfile();		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btn_startGame) {		
			for (int i = 0; i < ConfigManager.MAX_PLAYER; i++) {
				if (comboList[i].getSelectedIndex() != 0) {
					CharacterController.players[i] = ProfileManager.profiles.get(String.valueOf(comboList[i].getSelectedIndex()));
				}
				else {
					CharacterController.setNoOfPlayer(i);
					break;
				}
				//TODO: Fix 6 players bug
			}
			
			System.out.println(CharacterController.noOfPlayers);
			
			// TODO: Remove
			for (int i = 0; i < CharacterController.noOfPlayers; i++)
				System.out.println(CharacterController.players[i].toString());
				
			if (CharacterController.noOfPlayers > 1) {
				gc.getMapController2().setupMap(CharacterController.noOfPlayers);
				gc.getUiController().showGameFrame(CharacterController.noOfPlayers);
			}
			else {
				JOptionPane.showMessageDialog(null, "Please select at least 2 characters", "Message", 1);
			}
		}
		else if (e.getSource() == btn_createProfile) {
			gc.getUiController().showProfileCreateFrame();
		}
		
		//TODO hardcode char creation		
		
	}

	@Override
	public void itemStateChanged(ItemEvent e) {		
		if (comboList[comboActivateNum].getSelectedIndex() != 0)
			comboList[++comboActivateNum>5?comboActivateNum=5:comboActivateNum].setEnabled(true);			
	}
}
